﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using System.Windows.Threading;
using ImmunityBuster_wp7_2;
using TappUpsRefresh.Extras;
using System.IO;
using System.Windows.Media.Imaging;
using System.IO.IsolatedStorage;

namespace SlXnaApp3
{
    public partial class StartPage : PhoneApplicationPage
    {
        DispatcherTimer timer = new DispatcherTimer();
        double time = 0;
        bool currentState;
        
        public StartPage()
        {
            InitializeComponent();
            genderpicker.Items.Add("Male");
            genderpicker.Items.Add("Female");
            try
            {
                pb.Visibility = System.Windows.Visibility.Visible;
                WebClient web = new WebClient();
                web.BaseAddress = @"http://rushabh.greenpoison.org.in/countries";
                web.DownloadStringCompleted += new DownloadStringCompletedEventHandler(web_DownloadStringCompleted);
                web.DownloadStringAsync(new Uri(@"http://rushabh.greenpoison.org.in/countries"));


                SettingsClass settings = SettingsClass.Retrieve();
            }
            catch (NullReferenceException e)
            {

            }
            catch (WebException _e)
            {

            }

            timer.Interval = TimeSpan.FromSeconds(1);
            timer.Tick += new EventHandler(time_Tick);
            timer.Start();
            currentState = 
                (App.Current as App).firstStart;
          
        }

        void web_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
        {
            try
            {
                String s = e.Result;
                if (s.Contains("<p>Afghanistan"))
                {
                    int p = s.IndexOf("<p>Afghanistan");
                    int q = s.IndexOf("Zimbabwe</p>");
                    String temp = null;
                    for (int i = p + 3; i < q - 3; i++)
                    {
                        temp += s[i];
                    }
                    countrySelector.ItemsSource = temp.Split(' ').ToList();
                }
            }
            catch (Exception e1)
            {
                try
                {
                }
                catch (Exception e2)
                { }
            }
            pb.Visibility = System.Windows.Visibility.Collapsed;

        }

        void time_Tick(object sender, EventArgs e)
        {
            time++;
            if (time > 3 && time < 10)
            {
           
                
                ContentPanel.Visibility = System.Windows.Visibility.Visible;
                grid2.Visibility = System.Windows.Visibility.Collapsed;
                time = double.PositiveInfinity;
            }

            try
            {
                if (currentState)
                {
                    //Microsoft.Phone.Tasks.MediaPlayerLauncher mp = new Microsoft.Phone.Tasks.MediaPlayerLauncher();
                    //mp.Controls = Microsoft.Phone.Tasks.MediaPlaybackControls.All;
                    //mp.Location = Microsoft.Phone.Tasks.MediaLocationType.Install;
                    //mp.Media = new Uri("Content/HIV1.wmv", UriKind.RelativeOrAbsolute);
                    //mp.Show();
                
                    NavigationService.Navigate(new Uri("/MainPage1.xaml", UriKind.RelativeOrAbsolute));
                    timer.Stop();
                }
            }
            catch (NullReferenceException _e)
            { }
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            TappUpsRefresh.Extras.CheckForFirstStart.CommitFirstStart();                 
            NavigationService.Navigate(new Uri("/MainPage1.xaml", UriKind.RelativeOrAbsolute));
        }

        private void tb2_Loaded(object sender, RoutedEventArgs e)
        {
            

        }

        private void StackPanel_Tap(object sender, System.Windows.Input.GestureEventArgs e)
        {
            Microsoft.Phone.Tasks.PhotoChooserTask photo = new Microsoft.Phone.Tasks.PhotoChooserTask();
            photo.Completed += new EventHandler<Microsoft.Phone.Tasks.PhotoResult>(photo_Completed);
            photo.ShowCamera = true;
            photo.Show();

        }

        void photo_Completed(object sender, Microsoft.Phone.Tasks.PhotoResult e)
        {
            MemoryStream ms = new MemoryStream();
            e.ChosenPhoto.CopyTo(ms);
            byte[] temp = ms.GetBuffer();
            byte[] temp2 = temp;
            MemoryStream ms2 = new MemoryStream(temp);
            MemoryStream ms3 = new MemoryStream(temp2);
            BitmapImage bmp = new BitmapImage();
            bmp.SetSource(ms2);
            Image.Source = bmp;
            using (IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForApplication())
            {
                using (IsolatedStorageFileStream isfs = new IsolatedStorageFileStream(nameBox.Text.Trim() + ".jpg", FileMode.Create, isf))
                {
                     ms3.CopyTo(isfs);
                     isfs.Close();
                }               
            }
        
        }

        private void Button_Tap(object sender, System.Windows.Input.GestureEventArgs e)
        {
            //if ((nameBox.Text.Trim() == String.Empty) || (AgeBox.Value.Value == null) || (countrySelector.SelectedItem == null) || (genderpicker.SelectedItem == null))
            //{
            //    MessageBox.Show("Insufficient Information! \nCannot Proceed! ");
            //    return;
            //}
            try
            {
                Profile prof = new Profile();
                prof.Name = nameBox.Text;
                prof.Country = countrySelector.SelectedItem.ToString();
                prof.BirthDate = AgeBox.Value.Value;
                prof.Difficulty = "Easy";
                prof.ImageName = nameBox.Text.Trim() + ".jpg";
                prof.Score = 0;

                Profile.Save(prof);
                DefaultInfo inf = new DefaultInfo();
                inf.profileName = nameBox.Text.Trim();
                DefaultInfo.Save(inf);
                TappUpsRefresh.Extras.CheckForFirstStart.CommitFirstStart();
            }
            catch (Exception _e)
            { }
            //Microsoft.Phone.Tasks.MediaPlayerLauncher mp = new Microsoft.Phone.Tasks.MediaPlayerLauncher();
            //mp.Controls = Microsoft.Phone.Tasks.MediaPlaybackControls.All;
            //mp.Location = Microsoft.Phone.Tasks.MediaLocationType.Install;
            //mp.Media = new Uri("Content/HIV1.wmv", UriKind.RelativeOrAbsolute);
            //mp.Show();

            NavigationService.Navigate(new Uri("/MainPage1.xaml", UriKind.RelativeOrAbsolute));
        }
    }
}